﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace WXY.Course.DAL
{
    public static class DbExtension
    {
        public static T GetEntity<T>(this DbDataReader reader)
            where T : new()
        {
            T entity = new T();
            var properties = entity.GetType().GetProperties();
            foreach (var item in properties)
            {
                item.SetValue(entity, reader[item.Name], null);
            }
            return entity;
        }

        public static IList<T> GetEntityList<T>(this DbDataReader reader)
            where T : new()
        {
            IList<T> list = new List<T>();
            while (reader.Read())
            {
                T entity = GetEntity<T>(reader);
                list.Add(entity);
            }
            return list;
        }        
    }
}
